﻿package mortal.game.view.copy.escort
{
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.primitives.object2d.*;
    import frEngine.render.layer.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.layer3D.utils.*;
    import mortal.game.utils.*;
    import mortal.game.view.activeTime.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.tileList.*;
    import mortal.game.view.copy.escort.renderer.*;
    import mortal.game.view.copy.escort.vo.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.task.data.*;
    import mortal.game.view.vip.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class EscortModule extends EscortCopyGetTaskWindowBase
    {
        private var _bg:ScaleBitmap;
        private var _bossList:G3DTileList;
        private var _todayEscortNum:BitmapNumberText;
        private var _getTaskBtn:GLoadingButton;
        private var _refreshBtn:GLoadingButton;
        private var _goToNpcBtn:GLoadingButton;
        private var _btnFlyBoot:GLoadedButton;
        private var _autoBuy:GCheckBox;
        private var _brushingOut:GCheckBox;
        private var _color:GComboBox;
        private var _todayRefreshNum:GTextFiled;
        private var _colorTips:GTextFiled;
        private var _name:GImageBitmap;
        private var _btnSp:GSprite;
        private var _doubleCountDown:DoubleCountDownBox;
        private var _item:BaseItem;
        private var _addEscortNumBtn:GLoadedButton;
        private var _startX:Number;
        private var _endX:Number;
        private var _dp:Array;
        private var _data:SEscortInfo;
        private var _isAutoBuy:Boolean;
        private var _isRefreshToOrg:Boolean;
        private var _rect3d:Rect3DObject;
        private var _img2D:Img2D;
        public static var doubleEscortActive:SClientActive;
        private static var _instance:EscortModule;

        public function EscortModule(param1:ILayer = null)
        {
            this._dp = Language.getArray(70057);
            super(param1);
            setSize(902, 580);
            title = Language.getString(35190);
            this.layer = LayerManager.windowLayer3D;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.bg(7, 35, 895, 543, null);
            pushUIToDisposeVec(UIFactory.bg(2, 30, 906, 10, this.contentTopOf3DSprite, ImagesConst.SmallWindowBgLine));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.EscortBg1, 10, 45, this.contentTopOf3DSprite));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.EscortTxt1, 300, 45, this.contentTopOf3DSprite));
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.EscortMidBg, 311, 491, this.contentTopOf3DSprite));
            this._colorTips = UIFactory.gTextField(Language.getString(35191), 580, 55, 100, 20, this.contentTopOf3DSprite);
            this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(12, 40, 888, 540), this);
            Rect3DManager.instance.windowShowHander(null, this);
            this._img2D = new Img2D(null, this._bg.bitmapData, new Rectangle(0, 0, 888, 540), false);
            this._rect3d.addImg(this._img2D, Layer3DManager.backGroudMaskLayer);
            this._name = UIFactory.gImageBitmap("", 440, 48, this.contentTopOf3DSprite);
            this._bossList = UIFactory.g3dTileList(19, 51, 904, 550, this._rect3d, this.contentTopOf3DSprite);
            this._bossList.setStyle("cellRenderer", EscortTaskCellRenderer);
            this._bossList.offsetX = 7;
            this._bossList.offsetY = 1;
            this._bossList.columnWidth = 175;
            this._bossList.rowHeight = 415;
            this._bossList.configEventListener(Event.CHANGE, this.onListChangeHandler);
            var _loc_1:* = GlobalStyle.textFormatLv;
            _loc_1.size = 15;
            this._todayRefreshNum = UIFactory.gTextField("", 619, 493, 360, 60, contentTopOf3DSprite, _loc_1);
            this._todayRefreshNum.wordWrap = true;
            this._todayRefreshNum.multiline = true;
            this._todayEscortNum = UIFactory.bitmapNumberText(480, 502, "DefensePass2Num.png", 12, 22, 0, contentTopOf3DSprite, 14, BitmapNumberText.Mid);
            var _loc_2:* = ActiveTimeUtil.getTodaySClientActiveTimeTxt(EActiveType._EActiveTypeEscort);
            _loc_1.size = 14;
            pushUIToDisposeVec(UIFactory.gTextField(Language.getStringByParam(35192, _loc_2), 20, 490, 270, 25, this.contentTopOf3DSprite, _loc_1, true));
            this._btnSp = UICompomentPool.getUICompoment(GSprite);
            this.contentTopOf3DSprite.addChild(this._btnSp);
            this._doubleCountDown = UICompomentPool.getUICompoment(DoubleCountDownBox);
            this._doubleCountDown.countDownUrl = ImagesConst.DoubleContdowmEscort;
            this._doubleCountDown.startUrl = ImagesConst.doubleActiveRes_Escorting;
            this._doubleCountDown.endUrl = ImagesConst.doubleActiveRes_EscortEnd;
            this._doubleCountDown.x = 25;
            this._doubleCountDown.y = 514;
            contentTopOf3DSprite.addChild(this._doubleCountDown);
            this._addEscortNumBtn = UIFactory.gLoadedButton(ImagesConst.sub1_upSkin, 135 + 396, 500, 23, 23, this._btnSp);
            this._addEscortNumBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._addEscortNumBtn.toolTipData = Language.getStringByParam(35193, GameConst.ExtraEscortCost);
            this._getTaskBtn = UIFactory.gLoadingButton(ResFileConst.EscortBtn, 446, 530, 114, 33, this._btnSp);
            this._getTaskBtn.name = "escortGetTaskBtn";
            this._getTaskBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._refreshBtn = UIFactory.gLoadingButton(ResFileConst.EscortReFreshBtn, 331, 530, 114, 33, this._btnSp);
            this._refreshBtn.name = "escortRefreshBtn";
            this._refreshBtn.configEventListener(MouseEvent.CLICK, this.onClickHandler);
            this._goToNpcBtn = UIFactory.gLoadingButton(ResFileConst.EscortDoubleBtn, 400, 525, 108, 44, this.contentTopOf3DSprite);
            this._goToNpcBtn.configEventListener(MouseEvent.CLICK, this.gotoNpcHandler);
            this._btnFlyBoot = UIFactory.gLoadedButton(ImagesConst.MapBtnFlyBoot_upSkin, this._goToNpcBtn.x + this._goToNpcBtn.width + 10, this._goToNpcBtn.y + 10, 16, 18, contentTopOf3DSprite);
            this._btnFlyBoot.configEventListener(MouseEvent.CLICK, this.flyBootHandler);
            this._brushingOut = UIFactory.checkBox(Language.getString(70058), 766, 526, 200, 28, contentTopOf3DSprite);
            this._brushingOut.selected = this._isRefreshToOrg;
            this._autoBuy = UIFactory.checkBox(Language.getString(70054), 766, 551, 150, 28, contentTopOf3DSprite);
            this._autoBuy.selected = this._isAutoBuy;
            this._color = UIFactory.gComboBox(809, 532, 60, 20, new DataProvider(this._dp), contentTopOf3DSprite);
            this._color.selectedIndex = EColor._EColorOrange;
            var _loc_3:* = new ItemData(410221001);
            pushUIToDisposeVec(UIFactory.gTextField(Language.getString(35194) + ItemsUtil.getItemWholeName(_loc_3, true) + "*1", 620, 533, 80, 20, contentTopOf3DSprite, GlobalStyle.textFormatBai, true));
            pushUIToDisposeVec(UIFactory.gTextField(Language.getString(35195), 620, 555, 120, 20, contentTopOf3DSprite, GlobalStyle.textFormatBai));
            pushUIToDisposeVec(UIFactory.gBitmap(GameDefConfig.instance.getEPrictUnitImg(EPriceUnit._EPriceUnitCoinBind), 648, 555, contentTopOf3DSprite));
            this._item = UICompomentPool.getUICompoment(BaseItem);
            this._item.itemData = _loc_3;
            this._item.move(705, 533);
            contentTopOf3DSprite.addChild(this._item);
            this.updateBtnVible();
            this.updateLeftTime();
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.setTodayNum);
            NetDispatcher.addCmdListener(ServerCommand.VipLevelUp, this.setTodayNum);
            if (!GameController.autoGuide.hasGuideEscort())
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideActiveByEvent, {mainId:42, stepId:1}));
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._isAutoBuy = this._autoBuy.selected;
            this._isRefreshToOrg = this._brushingOut.selected;
            GuideAutoDoTask.isNotAutoDoTask = false;
            this._getTaskBtn.name = "";
            this._refreshBtn.name = "";
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.setTodayNum);
            NetDispatcher.removeCmdListener(ServerCommand.VipLevelUp, this.setTodayNum);
            this._btnFlyBoot.dispose(param1);
            this._btnFlyBoot = null;
            this._todayEscortNum.dispose(param1);
            this._todayEscortNum = null;
            this._bossList.dispose(param1);
            this._bossList = null;
            this._item.dispose(param1);
            this._item = null;
            this._btnSp.dispose(param1);
            this._btnSp = null;
            EffectManager.glowFilterUnReg(this._addEscortNumBtn);
            this._addEscortNumBtn.dispose(param1);
            this._addEscortNumBtn = null;
            EffectManager.glowFilterUnReg(this._getTaskBtn);
            this._getTaskBtn.dispose(param1);
            this._getTaskBtn = null;
            this._refreshBtn.dispose(param1);
            this._refreshBtn = null;
            this._autoBuy.dispose(param1);
            this._autoBuy = null;
            this._brushingOut.dispose(param1);
            this._brushingOut = null;
            this._color.dispose(param1);
            this._color = null;
            this._todayRefreshNum.dispose(param1);
            this._todayRefreshNum = null;
            this._goToNpcBtn.dispose(param1);
            this._goToNpcBtn = null;
            this._name.dispose(param1);
            this._name = null;
            this._bg.dispose(param1);
            this._bg = null;
            if (this._doubleCountDown)
            {
                this._doubleCountDown.dispose(param1);
                this._doubleCountDown = null;
            }
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
                this._img2D = null;
            }
            this._data = null;
            super.disposeImpl(param1);
            return;
        }// end function

        private function updateLeftTime() : void
        {
            var _loc_1:* = ActiveTimeUtil.getSClientActive(EActiveType._EActiveTypeEscort);
            this._doubleCountDown.sClientActive = _loc_1;
            return;
        }// end function

        override public function show(param1:int = 0, param2:int = 0) : void
        {
            super.show(param1, param2);
            this.updateBtnVible();
            if (!Cache.instance.copy.escort.isInDoubleEscort)
            {
                GameController.npc.curWindow = this;
            }
            return;
        }// end function

        public function updateBtnVible() : void
        {
            if (Cache.instance.copy.escort.isInDoubleEscort)
            {
                this._btnSp.visible = false;
                this._goToNpcBtn.visible = true;
                this._btnFlyBoot.visible = true;
            }
            else
            {
                this._btnSp.visible = true;
                this._goToNpcBtn.visible = false;
                this._btnFlyBoot.visible = false;
            }
            return;
        }// end function

        private function onTimeOutHandler() : void
        {
            return;
        }// end function

        private function flyBootHandler(event:MouseEvent) : void
        {
            if (Cache.instance.task.getGroupFinishCount(ETaskGroup._ETaskGroupEscort) >= 4)
            {
                MsgManager.showRollTipsMsg(Language.getString(35196), MsgRollTipsType.msgRollTips3);
            }
            else
            {
                AIManager.gotoOrSendtoNpc(1100008, true);
            }
            return;
        }// end function

        private function gotoNpcHandler(event:MouseEvent) : void
        {
            if (Cache.instance.task.getGroupFinishCount(ETaskGroup._ETaskGroupEscort) >= 4)
            {
                MsgManager.showRollTipsMsg(Language.getString(35196), MsgRollTipsType.msgRollTips3);
            }
            else
            {
                AIManager.gotoOrSendtoNpc(1100008, false);
            }
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            var _loc_2:EscortCopyOperVo = null;
            if (event.target == this._getTaskBtn)
            {
                if (this._data.todayNum == this._data.todayTotalNum && this._data.todayTotalNum == 3)
                {
                    this.addEscortNum();
                    return;
                }
                if (CopyUtil.isHasNomalEscortTask)
                {
                    TaskUtil.linkTask(Cache.instance.task.getTaskInfoByGroup(ETaskGroup._ETaskGroupEscort));
                    Cache.instance.npc.selectedNpc = null;
                    ThingUtil.selectEntity = null;
                    this.hide();
                    return;
                }
                _loc_2 = new EscortCopyOperVo(EEscortOper._EEscortOperGetEscort);
            }
            else if (event.target == this._refreshBtn)
            {
                if (this._data.todayNum == this._data.todayTotalNum && this._data.todayTotalNum == 3)
                {
                    this.addEscortNum();
                    return;
                }
                GuideAutoDoTask.isNotAutoDoTask = true;
                if (this._brushingOut.selected && (this._color.selectedIndex + 1) <= this._data.color)
                {
                    return MsgManager.showRollTipsMsg(Language.getString(35197), MsgRollTipsType.msgRollTips3);
                }
                _loc_2 = new EscortCopyOperVo(EEscortOper._EEscortOperRefresh);
                if (this._brushingOut.selected)
                {
                    _loc_2.extend = this._color.selectedIndex + 1;
                }
                _loc_2.autoBuy = this._autoBuy.selected;
            }
            else if (event.target == this._addEscortNumBtn)
            {
                this.addEscortNum();
                return;
            }
            if (_loc_2)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.EscortCopyOper, _loc_2));
            }
            return;
        }// end function

        private function addEscortNum() : void
        {
            var closeAlert:Function;
            closeAlert = function (param1:int) : void
            {
                var _loc_2:Object = null;
                if (param1 == Alert.OK)
                {
                    if (Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitGold, GameConst.ExtraEscortCost))
                    {
                        _loc_2 = new EscortCopyOperVo(EEscortOper._EEscortOperAddNum);
                        if (_loc_2)
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.EscortCopyOper, _loc_2));
                        }
                    }
                    else
                    {
                        MsgManager.showRollTipsMsg(Language.getString(35199), MsgRollTipsType.msgRollTips3);
                    }
                }
                return;
            }// end function
            ;
            var str:* = Language.getStringByParam(35198, GameConst.ExtraEscortCost);
            Alert.isFlowMouse = true;
            Alert.show(str, null, Alert.OK | Alert.CANCEL, null, closeAlert, null, 4);
            return;
        }// end function

        private function onListChangeHandler(event:Event) : void
        {
            if (this._bossList.selectedIndex != this._data.color)
            {
                this._bossList.selectedIndex = this._data.color;
            }
            return;
        }// end function

        public function updateInfo(param1:SEscortInfo) : void
        {
            this._data = param1;
            if (this._data.todayTotalNum == 4)
            {
                this._addEscortNumBtn.visible = false;
            }
            else
            {
                this._addEscortNumBtn.visible = true;
            }
            if (this._data.todayNum == this._data.todayTotalNum && this._data.todayNum == 3)
            {
                EffectManager.glowFilterReg(this._addEscortNumBtn);
            }
            else
            {
                EffectManager.glowFilterUnReg(this._addEscortNumBtn);
            }
            if (this._data.color == EColor._EColorOrange)
            {
                EffectManager.glowFilterReg(this._getTaskBtn);
                this._refreshBtn.visible = false;
                this._getTaskBtn.x = (this.width - this._getTaskBtn.width) * 0.5;
            }
            else
            {
                EffectManager.glowFilterUnReg(this._getTaskBtn);
                this._refreshBtn.visible = true;
                this._getTaskBtn.x = 465;
            }
            var _loc_2:* = this._data.color;
            var _loc_3:* = EscortColorConfig.instance.getItemByColor(_loc_2);
            this._name.imgUrl = "EscortColor_" + (_loc_3.id + 2);
            this._bossList.dataProvider = new DataProvider(EscortColorConfig.instance.allColorData);
            this._bossList.selectedIndex = _loc_2;
            this.setTodayNum();
            return;
        }// end function

        private function setTodayNum(param1:Object = null) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            if (this._data)
            {
                _loc_2 = int(VipConfig.instance.getValueByTypeAndLv(EVipRecordType._EVipRecordTypeEscortRefresh, VipUtil.vipLevel));
                if (_loc_2 > 0)
                {
                    this._todayRefreshNum.htmlText = Language.getStringByParam(70067, this._data.freeRefreshNum, 3 + _loc_2) + Language.getStringByParam(35200, _loc_2);
                }
                else
                {
                    _loc_3 = VipUtil.getVipMinLevelByType(EVipRecordType._EVipRecordTypeEscortRefresh);
                    _loc_2 = int(VipConfig.instance.getValueByTypeAndLv(EVipRecordType._EVipRecordTypeEscortRefresh, _loc_3));
                    this._todayRefreshNum.htmlText = Language.getStringByParam(70067, this._data.freeRefreshNum, 3) + Language.getStringByParam(35201, _loc_3, _loc_2);
                }
                this._todayEscortNum.text = this._data.todayNum + "/" + this._data.todayTotalNum;
                this._refreshBtn.enabled = this._data.todayNum < 3 + _loc_2;
                this._getTaskBtn.enabled = this._data.todayNum < 3 + _loc_2;
            }
            this.updateItemNum();
            return;
        }// end function

        public function updateItemNum(param1:Object = null) : void
        {
            var _loc_2:* = Cache.instance.pack.backPackCache.getItemCountByItemCode(this._item.itemData.itemCode);
            if (_loc_2 >= 1)
            {
                this._item.setAmountLabel(_loc_2.toString());
            }
            else
            {
                this._item.setAmountLabel("<font color=\'#df2121\'>" + _loc_2 + "</font>");
            }
            return;
        }// end function

        public static function get instance() : EscortModule
        {
            if (!_instance)
            {
                _instance = new EscortModule;
            }
            return _instance;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

        public static function get hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
